package com.creditrank.indexer;
import java.sql.*;
import java.io.*;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;

import java.util.LinkedList;
import java.util.Random;

public class Db_Retrieve_thread
{
	
	
	public Db_Retrieve_thread()
	{
		
		Thread_create(0,100000,"first",1);
		Thread_create(100001,200001,"second",2);
		Thread_create(200001,300000,"Third",3);
		Thread_create(300001,400000,"Fourth",4);
		Thread_create(400001,550000,"Fifth",5);
		
	}
	
	public void Thread_create(final int start,final int end,String name, final int i)
	{
		
		Thread Th1= new Thread(name)
	    {
	    	public void run()
		{
			//System.out.println("Send thread started at Routing layer");
	    		File index=new File("Code1/index"+i);
	    		IndexWriter writer = null;
				
					try 
					{
						writer = new IndexWriter(FSDirectory.open(index), new StandardAnalyzer(Version.LUCENE_CURRENT), true, new IndexWriter.MaxFieldLength(1000000));
					} 
					catch (CorruptIndexException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					catch (LockObtainFailedException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
					catch (IOException e) 
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
	    		
			connect(start,end,writer,i);
		}
	    };
	    System.out.println("Thread "+i+" Started");
	    Th1.start();
	}
	
	
	public void connect(int count, int end, IndexWriter writer,int i)
	{
		
		String dbtime="";
		Blob dbdata;
		String s1=null,s2=null;
		double d=0.0;
		//IndexWriter writer = null;
		Random gen=new Random();
		SeralizeHash sh=new SeralizeHash();
		
	//	String dbUrl = "jdbc:mysql://mysql.cis.ksu.edu:3306/nagarjun";
		String dbUrl = "jdbc:mysql://mysql.cis.ksu.edu:3306/sudhikrr";
		//long i=1;
		try 
		{

			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection (dbUrl,"sudhikrr","40Vlf0bK");
			String sb1="";
			
			for(;count<=end;count=count+1)

			{
				String sql = "SELECT id,url,content,graph FROM test5 where id between "+count+" and "+(count+100);
				//String sql = "SELECT id,text FROM test2 where id="+j;
				
				PreparedStatement prestmnt = con.prepareStatement(sql);
				ResultSet rs = prestmnt.executeQuery();
				while (rs.next()) 
				{
					s1=rs.getString(1);
					dbtime = rs.getString(2);
					dbdata=rs.getBlob(3);
					s2=rs.getString(4);
					d=gen.nextDouble();
					LinkedList<String> l =new LinkedList<String>();
					l.add(dbtime);
					l.add(s2);
					l.add(Double.toString(d));
					sh.outgoing.put(s1,l);
				//	sh.id_url.put(s1,dbtime);
									
					try
					{
						InputStream x=dbdata.getBinaryStream();
						int size=x.available();
						byte b[]= new byte[size];
					
						x.read(b);
						sb1=new String(b);
						Document doc = HTMLdoc.Document(dbtime,sb1);
						System.out.println("adding " + doc.get("path"));
			       		writer.addDocument(doc);
			       		sb1="";
															
					
					}
					catch (IOException ie)
					{
						System.out.println(ie);
					}
										
			} 
				
				System.out.println(count);
				count=count+100;
		    }	
			con.close();
			//System.out.println(i);
			writer.optimize();
			writer.close();
			Db_Retrieve_thread.serialize_hash(i,sh);
		} //end try
		catch(ClassNotFoundException e) 
		{
			e.printStackTrace();
		}

		catch(SQLException e) 
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}  //end main
		

	}  //end method
	
	public static void serialize_hash(int i,SeralizeHash sh) throws IOException
	{
		FileOutputStream fos = new FileOutputStream( "Code1/Serialized"+i+".txt" );
		ObjectOutputStream outStream =new ObjectOutputStream( fos );
		
	      //  Save each object.
	      outStream.writeObject( sh );
	      outStream.close();



	}
	public static void main(String[] args) throws Exception
	{
		//ic= new IndexCreate();
		Db_Retrieve_thread db= new Db_Retrieve_thread();
		//new MergeIndex();
		//db.connect();
		//db.serialize_hash();
		//ic.writer.close();
		//ic.writer.optimize();
	}

	
	
}

